
package com.insigma.business.qrcode.soldier.service.impl;

import com.insigma.business.qrcode.common.service.SequenceGenerator;
import com.insigma.business.qrcode.soldier.dao.SoldierLabelInfoDao;
import com.insigma.business.qrcode.soldier.dto.SequenceNameConstant;
import com.insigma.business.qrcode.soldier.entity.SoldierLabelInfoDO;
import com.insigma.business.qrcode.soldier.request.SoldierLabelInfoBSearcher;
import com.insigma.business.qrcode.soldier.service.SoldierLabelInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("soldierLabelInfoService")
public class SoldierLabelInfoServiceImpl implements SoldierLabelInfoService {

    @Autowired(required = false)
    private SoldierLabelInfoDao soldierLabelInfoDao;
    @Autowired(required = false)
    private SequenceGenerator sequenceGenerator;

    
    @Override
    public List<SoldierLabelInfoDO> search(final SoldierLabelInfoBSearcher soldierLabelInfoBSearcher) {
        return soldierLabelInfoDao.search(soldierLabelInfoBSearcher);
    }
   
    @Override
    public SoldierLabelInfoDO get(final String id){
        return soldierLabelInfoDao.get(id);
    }

    
    @Override
    public Integer insert(final SoldierLabelInfoDO soldierLabelInfoDO){
        soldierLabelInfoDO.setId(sequenceGenerator.genKey(SequenceNameConstant.SOLDIER_LABEL_INFO_ID));
        return soldierLabelInfoDao.insert(soldierLabelInfoDO);
    }

    
    @Override
    public Integer update(final SoldierLabelInfoDO soldierLabelInfoDO){
        return soldierLabelInfoDao.update(soldierLabelInfoDO);
    }

    
    @Override
    public Integer delete(final String id, final String modifierType, final String modifierId){
        return soldierLabelInfoDao.delete(id,modifierType,modifierId);
    }

    @Override
    public Integer updateLabels(String idCardNo, String labels) {
        soldierLabelInfoDao.deleteByIdCardNo(idCardNo);
        if (Strings.isNotBlank(labels)) {
            String[] split = labels.split(",");
            for (String s : split) {
                SoldierLabelInfoDO infoDO = new SoldierLabelInfoDO();
                infoDO.setIdCardNo(idCardNo);
                infoDO.setName(s.trim());
                insert(infoDO);
            }
            return split.length;
        }
        return 0;
    }

}
